/**
 * 
 */
package ru.olympus.antonyhands.ao.client.widgets;

import ru.olympus.antonyhands.ao.client.AOHelperServiceAsync;
import ru.olympus.antonyhands.ao.client.UIHelper;
import ru.olympus.antonyhands.ao.shared.util.StringUtilities;

import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;

/**
 * @author AntonyHands
 * @since 28.02.2012
 */
public class AddItemVerticalPanel extends VerticalPanel
{
    private AOHelperServiceAsync _service;
    private Label errorLabel;
    private TextBox codeField;
    private TextBox titleField;
    private ItemCategoriesListBox itemCategories;
    private TextBox fixedPrice;
    private CraftSkillListBox craftSkills;
    private ItemCraftTable2 craftTable;
    private Button okButton;
    private Button closeButton;

    public AddItemVerticalPanel(AOHelperServiceAsync service)
    {
        _service = service;
        add(new Label("Добавление предмета"));

        errorLabel = new Label();
        errorLabel.setStyleName("serverResponseLabelError");
        add(errorLabel);

        Grid table = new Grid(6, 2);
        table.setHTML(0, 0, "Код");
        table.setHTML(1, 0, "Название");
        table.setHTML(2, 0, "Категория");
        table.setHTML(3, 0, "Фиксированная цена");
        table.setHTML(4, 0, "Навык производства");
        add(table);

        codeField = new TextBox();
        table.setWidget(0, 1, codeField);

        titleField = new TextBox();
        table.setWidget(1, 1, titleField);

        itemCategories = new ItemCategoriesListBox(_service);
        table.setWidget(2, 1, itemCategories);

        fixedPrice = new TextBox();
        table.setWidget(3, 1, fixedPrice);

        craftSkills = new CraftSkillListBox(_service, true);
        table.setWidget(4, 1, craftSkills);

        craftTable = new ItemCraftTable2(_service);
        table.setWidget(5, 1, craftTable);
        craftTable.setVisible(!craftSkills.isEmptyOption());

        craftSkills.addChangeHandler(new ChangeHandler()
        {
            @Override
            public void onChange(ChangeEvent event)
            {
                craftTable.setVisible(!craftSkills.isEmptyOption());
            }
        });

        HorizontalPanel buttonsPanel = new HorizontalPanel();
        buttonsPanel.addStyleName("dialogHPanel");
        add(buttonsPanel);

        okButton = new Button("OK");
        buttonsPanel.add(okButton);
        okButton.addClickHandler(new ClickHandler()
        {
            @Override
            public void onClick(ClickEvent event)
            {
                errorLabel.setText("");
                _service.addItem(codeField.getText(), titleField.getText(), 
                        itemCategories.getCurrentValueCode(), 
                        fixedPrice.getText(), craftSkills.getCurrentValueCode(), 
                        craftTable.getCraftLevel(),
                        craftTable.getWorkload(), craftTable.getCraftQuantity(),
                        craftTable.listResources(),
                        new AsyncCallback<String>()
                        {
                            public void onFailure(Throwable caught)
                            {
                                errorLabel.setText("Ошибка при работе с сервисом");
                            }

                            @Override
                            public void onSuccess(String result)
                            {
                                if (StringUtilities.isEmpty(result))
                                    RootPanel.get(UIHelper.INFO_DIV).clear();
                                else
                                    errorLabel.setText(result);
                            }
                        });
            }
        });
        closeButton = new Button("Отмена");
        buttonsPanel.add(closeButton);

        closeButton.addClickHandler(new ClickHandler()
        {
            public void onClick(ClickEvent event)
            {
                RootPanel.get(UIHelper.INFO_DIV).clear();
            }
        });
    }

}
